﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utilities;
using Web;
using System.Data;
using Domain.DAL;

namespace WebApp.ReportsAndStatistics
{
    public partial class ReportByProductTypes : System.Web.UI.Page
    {
        #region Private Fields

        private string _preUrl = "~/ReportsAndStatistics/ReportByProductTypes.aspx";
        private string _cookiesName = string.Empty;

        #endregion

        #region Private Properties

        public string ReportDateFrom
        {
            get
            {
                if (rdpReportDateFrom.SelectedDate.HasValue && rdpReportDateFrom.SelectedDate.Value != null)
                {
                    return rdpReportDateFrom.SelectedDate.Value.ToString(MyConfig.DateFormat);
                }
                return string.Empty;
            }
            set
            {
                try
                {
                    rdpReportDateFrom.SelectedDate = DateTime.ParseExact(value, MyConfig.DateFormat, null);
                }
                catch (Exception)
                {
                    rdpReportDateFrom.SelectedDate = null;
                }
            }
        }

        public string ReportDateTo
        {
            get
            {
                if (rdpReportDateTo.SelectedDate.HasValue && rdpReportDateTo.SelectedDate.Value != null)
                {
                    return rdpReportDateTo.SelectedDate.Value.ToString(MyConfig.DateFormat);
                }
                return string.Empty;
            }
            set
            {
                try
                {
                    rdpReportDateTo.SelectedDate = DateTime.ParseExact(value, MyConfig.DateFormat, null);
                }
                catch (Exception)
                {
                    rdpReportDateTo.SelectedDate = null;
                }
            }
        }

        #endregion

        #region Event Handlers

        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                (Master as ReportsAndStatisticsPages).SelectedTab = "reportsAndStatistics";
                _cookiesName = this.GetType().Name + Session.SessionID;

                if (!IsPostBack)
                {
                    LoadForm();
                    BindReportsAndStatistics();
                }
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void rptResult_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            try
            {
                if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
                {
                    //Set navigate url for hyperlink fullname
                    Label lblProductTypeID = (Label)e.Item.FindControl("lblProductTypeID");
                    HyperLink hplProductTypeName = (HyperLink)e.Item.FindControl("hplProductTypeName");
                    if (lblProductTypeID != null && hplProductTypeName != null)
                    {
                        hplProductTypeName.NavigateUrl = this.ResolveUrl("~/ReportsAndStatistics/ReportByProductTypesDetails.aspx") + "?productTypeID=" + lblProductTypeID.Text + "&reportDateFrom=" + this.ReportDateFrom + "&reportDateTo=" + this.ReportDateTo + "&preUrl=" + this._preUrl;
                    }

                    //Format quantity
                    Label lblQuantity = (Label)e.Item.FindControl("lblQuantity");
                    if (lblQuantity != null)
                    {
                        lblQuantity.Text = Utility.FormatNumber(lblQuantity.Text, MyConfig.NumberFormat, TypeCode.Int64);
                    }

                    //Format warranty quantity
                    Label lblWarrantyQuantity = (Label)e.Item.FindControl("lblWarrantyQuantity");
                    if (lblWarrantyQuantity != null)
                    {
                        lblWarrantyQuantity.Text = Utility.FormatNumber(lblWarrantyQuantity.Text, MyConfig.NumberFormat, TypeCode.Int64);
                    }

                    //Format no warranty quantity
                    Label lblNoWarrantyQuantity = (Label)e.Item.FindControl("lblNoWarrantyQuantity");
                    if (lblNoWarrantyQuantity != null)
                    {
                        lblNoWarrantyQuantity.Text = Utility.FormatNumber(lblNoWarrantyQuantity.Text, MyConfig.NumberFormat, TypeCode.Int64);
                    }

                    //Format cost
                    Label lblCost = (Label)e.Item.FindControl("lblCost");
                    if (lblCost != null)
                    {
                        lblCost.Text = Utility.FormatNumber(lblCost.Text, MyConfig.CurrencyFormat, TypeCode.Decimal);
                    }

                }
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void btnReport_Click(object sender, EventArgs e)
        {
            try
            {

                CookiesManager.Update(this._cookiesName, rdpReportDateFrom.ID, this.ReportDateFrom);
                CookiesManager.Update(this._cookiesName, rdpReportDateTo.ID, this.ReportDateTo);

                BindReportsAndStatistics();
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        #endregion

        #region Private Methods

        private void BindReportsAndStatistics()
        {
            try
            {
                DataSet ds = new DataSet();

                string reportDateFrom = this.ReportDateFrom;
                string reportDateTo = this.ReportDateTo;

                ReportsAndStatisticsDAO rsDAO = ReportsAndStatisticsDAO.GetInstance();
                if (rsDAO != null)
                {
                    ds = rsDAO.ReportByProductType(reportDateFrom, reportDateTo);
                    if (ds != null && ds.Tables.Count > 1)
                    {
                        if (ds.Tables[ds.Tables.Count - 1].Rows.Count > 0)
                        {
                            if (ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalQuantity"] != null && ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalQuantity"] != DBNull.Value)
                            {
                                lblTotalQuantity.Text = Utility.FormatNumber(ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalQuantity"].ToString(), MyConfig.NumberFormat, TypeCode.Int64);
                            }
                            if (ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalWarrantyQuantity"] != null && ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalWarrantyQuantity"] != DBNull.Value)
                            {
                                lblTotalWarrantyQuantity.Text = Utility.FormatNumber(ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalWarrantyQuantity"].ToString(), MyConfig.NumberFormat, TypeCode.Int64);
                            }
                            if (ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalNoWarrantyQuantity"] != null && ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalNoWarrantyQuantity"] != DBNull.Value)
                            {
                                lblTotalNoWarrantyQuantity.Text = Utility.FormatNumber(ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalNoWarrantyQuantity"].ToString(), MyConfig.NumberFormat, TypeCode.Int64);
                            }
                            if (ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalCost"] != null && ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalCost"] != DBNull.Value)
                            {
                                lblTotalCost.Text = Utility.FormatNumber(ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalCost"].ToString(), MyConfig.CurrencyFormat, TypeCode.Decimal);
                            }

                        }
                        rptResult.DataSource = ds.Tables[0];
                        rptResult.DataBind();
                    }
                    
                }

            }
            catch (Exception)
            {
                throw;
            }
        }

        private void LoadForm()
        {
            try
            {
                (Master as ReportsAndStatisticsPages).PageTitle = Resources.Strings.ReportByProductTypes;

                this.ReportDateTo = DateTime.Now.ToString(MyConfig.DateFormat);
                this.ReportDateFrom = DateTime.Now.AddDays(Convert.ToInt32(GetParameterValue(MyConfig.ReportDateAgoCode, "-30"))).ToString(MyConfig.DateFormat);

                //Get stored value from cookies
                if (CookiesManager.GetValue(this._cookiesName, rdpReportDateFrom.ID) != null)
                {
                    this.ReportDateFrom = CookiesManager.GetValue(this._cookiesName, rdpReportDateFrom.ID);
                }
                if (CookiesManager.GetValue(this._cookiesName, rdpReportDateTo.ID) != null)
                {
                    this.ReportDateTo = CookiesManager.GetValue(this._cookiesName, rdpReportDateTo.ID);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private string GetParameterValue(string code, string defaultValue)
        {
            string value = defaultValue;
            try
            {
                Domain.DAL.ParameterDAO pDAO = Domain.DAL.ParameterDAO.GetInstance();
                if (pDAO != null)
                {
                    Domain.Models.Parameter p = new Domain.Models.Parameter();

                    p = pDAO.GetByCode(code);
                    if (p != null && !string.IsNullOrEmpty(p.Value))
                    {
                        value = p.Value;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return value;
        }

        #endregion

    }
}